package com.arcvideo.acsconnect.app;

import android.text.TextUtils;

import com.arcvideo.acsconnect.rest.model.WXAccessToken;
import com.arcvideo.acsconnect.rest.model.WXUserInfo;
import com.framework.core.utils.SPUtil;
import com.google.gson.Gson;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/25
 * @description
 */

public class UserDataCenter {

    public final static String KEY_USER_INFO_BEAN = "KEY_USER_INFO_BEAN";
    public final static String KEY_WX_ACCESS_TOKEN = "KEY_WX_ACCESS_TOKEN";
    public final static String KEY_RTC_ADDRESS = "KEY_RTC_ADDRESS";
    public final static String KEY_IM_ADDRESS = "KEY_IM_ADDRESS";

//    public final static String KEY_USER_1= "KEY_USER_1";
//    public final static String KEY_USER_2= "KEY_USER_2";

    public static boolean sIsWxLogin = false;

//    public final static String KEY_IM_SERVICE_BEAN = "KEY_IM_SERVICE_BEAN";

    public static UserDataCenter mInstance;
    private WXUserInfo mUserInfoBean;
    private WXAccessToken mWXAccessToken;
//    private String mRtcAddress;

    private UserDataCenter() {

    }

    public static UserDataCenter getInstance() {
        if (null == mInstance) {
            mInstance = new UserDataCenter();
        }
        return mInstance;
    }

    public void cleanUserInfoBean() {
        this.mUserInfoBean = null;
        SPUtil.put(AcsConnectApplication.getInstance(), KEY_USER_INFO_BEAN, "");
    }

    public void setUserInfoBean(WXUserInfo userInfoBean) {
        if (null == userInfoBean) {
            return;
        }
        this.mUserInfoBean = userInfoBean;
        String loginBeanStr = new Gson().toJson(userInfoBean);
        SPUtil.put(AcsConnectApplication.getInstance(), KEY_USER_INFO_BEAN, loginBeanStr);
    }

    public WXUserInfo getUserInfoBean() {
        if (null != mUserInfoBean) {
            return mUserInfoBean;
        }
        String userInfoBeanStr = SPUtil.getString(AcsConnectApplication.getInstance(), KEY_USER_INFO_BEAN);
        WXUserInfo userInfoBean = new Gson().fromJson(userInfoBeanStr, WXUserInfo.class);
        this.mUserInfoBean = userInfoBean;
        return mUserInfoBean;
    }

    public void setWXAccessToken(WXAccessToken wxAccessToken) {
        if (null == wxAccessToken) {
            return;
        }
        this.mWXAccessToken = wxAccessToken;
        String wxAccessTokenStr = new Gson().toJson(wxAccessToken);
        SPUtil.put(AcsConnectApplication.getInstance(), KEY_WX_ACCESS_TOKEN, wxAccessTokenStr);
    }

    public WXAccessToken getWXAccessToken() {
        if (null != mWXAccessToken) {
            return mWXAccessToken;
        }
        String wXAccessTokenStr = SPUtil.getString(AcsConnectApplication.getInstance(), KEY_WX_ACCESS_TOKEN);
        WXAccessToken wxAccessToken = new Gson().fromJson(wXAccessTokenStr, WXAccessToken.class);
        this.mWXAccessToken = wxAccessToken;
        return mWXAccessToken;
    }

    public void setRtcAddress(String rtcAddress) {
        SPUtil.put(AcsConnectApplication.getInstance(), KEY_RTC_ADDRESS, rtcAddress);
    }

    public String getRtcAddress() {
        String rtcAddress = SPUtil.getString(AcsConnectApplication.getInstance(), KEY_RTC_ADDRESS);
        return rtcAddress;
    }

    public void setImAddress(String imAddress) {
        SPUtil.put(AcsConnectApplication.getInstance(), KEY_IM_ADDRESS, imAddress);
    }

    public String getImAddress() {
        String imAddress = SPUtil.getString(AcsConnectApplication.getInstance(), KEY_IM_ADDRESS);
        if (TextUtils.isEmpty(imAddress)) {
            imAddress = GlobalConstant.sImAddress;
            setImAddress(imAddress);
        }
        return imAddress;
    }

}
